package com.example.dbdesign.service.impl;

import com.example.dbdesign.mapper.RolePermissionMapper;
import com.example.dbdesign.mapper.UserMapper;
import com.example.dbdesign.pojo.entity.User;
import com.example.dbdesign.pojo.vo.LoginUser;
import com.example.dbdesign.pojo.vo.Res;
import com.example.dbdesign.utils.JwtUtil;
import com.example.dbdesign.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public UserDetails loadUserByUsername(String uno) throws UsernameNotFoundException {
        return loadUserByUno(uno);
    }

    public UserDetails loadUserByUno(String uno) throws UsernameNotFoundException {

        User user = userMapper.queryByUno(uno);
        if(user == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        // 添加权限
        System.out.println(user + " here");
        List<String> list = rolePermissionMapper.queryPermsByUserId(user.getId());
        System.out.println(list);
//        List<String> list = new ArrayList<>(Arrays.asList("test"));
//        System.out.println(user.getUno());
        return new LoginUser(user, list);
    }

}
